package main

import (
	"flag"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/crypto"
	"log"
	"strings"
)

var (
	Symmetric    = make(map[string]crypto.SymmetricCrypto)
	CryptoType   = ""
	CryptoKey    = ""
	CryptoMethod = ""
	Data         = ""
)

func initCrypto() {
	Symmetric["aes"] = crypto.AES{}
	Symmetric["sm4"] = crypto.SM4{}
}

func main() {
	flag.StringVar(&CryptoType, "type", "", "aes/sm4")
	flag.StringVar(&CryptoKey, "key", "", "")
	flag.StringVar(&CryptoMethod, "method", "", "encrypt/decrypt")
	flag.StringVar(&Data, "data", "", "")
	flag.Parse()
	initCrypto()
	CryptoType = strings.ToLower(CryptoType)
	CryptoMethod = strings.ToLower(CryptoMethod)
	crypto, ok := Symmetric[CryptoType]
	if !ok {
		log.Printf("invalid type, [%s]\n", CryptoType)
		log.Println("run --help")
		return
	}
	switch CryptoMethod {
	case "encrypt":
		base64Code, err := crypto.EncryptWithBase64([]byte(Data), []byte(CryptoKey))
		if err != nil {
			log.Printf("%s %s fail, %v\n", CryptoType, CryptoMethod, err)
			return
		}
		log.Printf("raw    = [%s]\n", Data)
		log.Printf("crypto = [%s]\n", base64Code)
	case "decrypt":
		raw, err := crypto.DecryptWithBase64(Data, []byte(CryptoKey))
		if err != nil {
			log.Printf("%s %s fail, %v\n", CryptoType, CryptoMethod, err)
			return
		}
		log.Printf("crypto = [%s]\n", Data)
		log.Printf("raw    = [%s]\n", raw)
	default:
		log.Println("run --help")
	}
}
